package com.ayoka.mvp;

import android.support.v4.app.FragmentManager;

import com.ayoka.R;
import com.ayoka.base.BasePresenter;
import com.ayoka.base.IBaseView;
import com.ayoka.bean.BaseResponse;
import com.ayoka.http.API;
import com.ayoka.http.OkHttpListener;
import com.ayoka.mvp.TrainingDetailMVP.TrainingDetailModel.ResponseTrainingDetailBean;
import com.ayoka.utils.MediaManager;
import com.ayoka.widget.OneButtonDialog;
import com.ayoka.widget.TwoButtonDialog;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.dialog.DialogResultListener;
import com.jcp.baselibrary.utils.UIUtils;

import java.io.Serializable;
import java.util.List;

/**
 * Created by gabriel on 2017/3/5.
 */

public interface TrainingDetailMVP {

    interface TrainingDetailModel {

        class ResponseTrainingDetailBean implements Serializable {
            public TrainingDetailBean meditation;
            public long record_id;
        }

        class TrainingDetailBean implements Serializable {
            public long ID;
            public long PackageID;
            public long CourseID;
            public int CourseCompleted;
            public int IsAvailabe;
            public String Title;
            public String SubTitle;
            public String ImgUrl;
            public String SoundUrl;

            public List<TrainingQuestionBean> Questions;
        }

        class TrainingQuestionBean implements Serializable {
            public long ID;
            public long MeditationID;
            public String Question;
            public int QuestionType;
            public List<TrainingQuestionOptionBean> Options;
        }

        class TrainingQuestionOptionBean implements Serializable {
            public long Index;
            public String Option;
        }
    }

    interface ITrainingDetailView extends IBaseView {

        void configTrainingDetailData(ResponseTrainingDetailBean responseRrainingDetailBean);

        void onQuitClick();

        void onOperationSuccess();
    }

    class TrainingDetailPresenter extends BasePresenter<ITrainingDetailView> {

        public void getTrainingDetailData(long trainingId) {

            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.getTrainingDetailData(this, trainingId, new OkHttpListener<BaseResponse<ResponseTrainingDetailBean>>() {
                @Override
                public void onSuccess(BaseResponse<ResponseTrainingDetailBean> response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().configTrainingDetailData(response.data);
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        public void AddMeditationRecord(long recordId) {
            mViewRef.get().showWaitDialog(UIUtils.getString(R.string.loading));
            API.AddMeditationRecord(this, recordId, new OkHttpListener<BaseResponse>() {
                @Override
                public void onSuccess(BaseResponse response) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().onOperationSuccess();
                }

                @Override
                public void onFailure(int errorCode, String errorMessage) {
                    if (!isAttached()) return;
                    mViewRef.get().hideWaitDialog();
                    mViewRef.get().showToast(errorMessage);
                }
            });
        }

        public void showStartMeditationDialog(FragmentManager fragmentManager) {
            OneButtonDialog dialog = new OneButtonDialog.Builder()
                    .setTitle(UIUtils.getString(R.string.point_out))
                    .setMessage(UIUtils.getString(R.string.meditation_need_quite_place))
                    .setPositiveText(UIUtils.getString(R.string.start_training))
                    .build();
            dialog.show(fragmentManager, "OneButtonDialog");
            MediaManager.getInstance().pause();
        }

        public void showConfirmQuitMeditationDialog(FragmentManager fragmentManager, String message) {
            TwoButtonDialog dialog = new TwoButtonDialog.Builder()
                    .setTitle(UIUtils.getString(R.string.point_out))
                    .setMessage(message)
                    .setNegativeText(UIUtils.getString(R.string.cancel))
                    .setPositiveText(UIUtils.getString(R.string.confirm))
                    .build();
            dialog.setDialogResultListener(new DialogResultListener<Boolean>() {
                @Override
                public void result(BaseDialogFragment DialogFragment, Boolean result) {
                    if (!isAttached()) return;
                    if (result) {
                        mViewRef.get().onQuitClick();
                    } else {
                        MediaManager.getInstance().resume();
                    }
                }
            });
            dialog.show(fragmentManager, "TwoButtonDialog");
            MediaManager.getInstance().pause();
        }
    }
}
